package com.deer.wms.kuaidiniao.service.impl;

import com.deer.wms.kuaidiniao.constant.KuaidiniaoConstant;
import com.deer.wms.kuaidiniao.model.PrintParams;
import com.deer.wms.kuaidiniao.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;

@Service
@Transactional
public class CloudPrintApi {
    /**
     * 向快递鸟发送打印参数
     * @param expCode
     * @param expNo
     * @param isPreview
     * @throws Exception
     */
    public String sendPrintData(String expCode, String expNo, Integer isPreview) throws Exception {
//        String printParams = getPrintParam(KuaidiniaoConstant.printerPcIp, expCode, expNo, KuaidiniaoConstant.printerName, isPreview);
//
//        String result = Utils.sendPost2(KuaidiniaoConstant.cloudPrintURL, printParams);
//        System.out.println("result::::"+result);
        return "";
    }
    /**
     * get print order param to json string
     * @return
     *
     * @throws Exception
     */
    public String getPrintParamJson(String printerPcIp, String expCode, String expNo, String printerName, Integer isPreview) throws Exception {
        String data = "[{\"OrderCode\":\""+expNo+"\",\"PortName\":\""+printerName+"\"}]";
        String result = "{\"RequestData\": \"" + URLEncoder.encode(data, "UTF-8") + "\", \"EBusinessID\":\"" + KuaidiniaoConstant.EBusinessID + "\", \"DataSign\":\"" + Utils.encrypt(printerPcIp + data, KuaidiniaoConstant.AppKey, "UTF-8") + "\", \"IsPreview\":\""
                + isPreview + "\"}";
        return result;
    }

    /**
     * get print order param to object
     * @return
     *
     * @throws Exception
     */
    public PrintParams getPrintParam(String printerPcIp, String expCode, String expNo, String printerName, Integer isPreview) throws Exception {
        String data = "[{\"OrderCode\":\""+expNo+"\",\"PortName\":\""+printerName+"\"}]";
        PrintParams printParams = new PrintParams();
        printParams.setRequestData(URLEncoder.encode(data, "UTF-8"));
        printParams.setEBusinessID(KuaidiniaoConstant.EBusinessID);
        printParams.setDataSign(Utils.encrypt(printerPcIp + data, KuaidiniaoConstant.AppKey, "UTF-8"));
        printParams.setIsPreview(isPreview.toString());
        return printParams;
    }

    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     * @throws IOException
     */
    public final static String getIpAddress(HttpServletRequest request) throws IOException {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }
}
